package com.mars.weixin.web.boss.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.web.HttpCode;
import com.mars.base.common.utils.web.ResponseResult;
import com.mars.base.web.BaseController;
import com.mars.boss.weixin.entity.WxMenu;
import com.mars.boss.weixin.exception.WxMenuBizException;
import com.mars.boss.weixin.service.WxMenuService;
import com.mars.boss.weixin.vo.Menu;
import com.mars.weixin.api.Api;
import com.mars.weixin.api.ResponseMsg;
import com.mars.weixin.common.WeixinContext;
import com.mars.weixin.common.WeixinUtil;

@RestController
@RequestMapping("/weixin/menu")
public class WxMenuController extends BaseController {

	@Autowired
	private WxMenuService service;
	
	@PostMapping(value = "/add")
	public Object add(WxMenu entity) {
		try {
			this.service.insert(entity) ;
		} catch (AppException e) {
			return ResponseResult.addFailed();
		}
		return ResponseResult.addOk();
	}
	
	@GetMapping(value = "/delete/{ids}")
	public Object delete(@PathVariable List<Long> ids) {
		try {
			this.service.deleteByIds(ids);
		} catch (WxMenuBizException e) {
			return ResponseResult.deleteFailed();
		}
		return ResponseResult.deleteOk();
	}
	
	@PostMapping(value = "/update")
	public Object update(WxMenu entity) {
		try {
			this.service.update(entity) ;
		} catch (AppException e) {
			return ResponseResult.updateFailed();
		}
		return ResponseResult.updateOk();
	}
	
	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Long id) {
		try {
			return this.service.selectById(id) ;
		} catch (AppException e) {
			return ResponseResult.loadFailed();
		}
	}
	
	@GetMapping(value = "/list")
	public Object list(WxMenu entity) {
		return ResponseResult.loadOk(this.service.selectList(entity));
	}
	
	@GetMapping(value = "/tree")
	public Object tree(WxMenu entity) {
		return this.service.tree();
	}
	
	@GetMapping(value = "/treegrid")
	public Object treegrid(WxMenu entity) {
		return this.service.treegrid();
	}

	@GetMapping(value = "/clean")
	public Object clear() {
		Long count = this.service.clearCache() ;
		return ResponseResult.build(HttpCode.CLEAR_CACHE, count);
	}
	
	@PostMapping(value = "/status")
	public Object disabledMenu(Long id) {
		try {
			this.service.status(id);
		} catch (WxMenuBizException e) {
			return ResponseResult.failure() ;
		}
		return ResponseResult.ok() ;
	}
	
	@GetMapping(value="/push")
	public Object pushMenuToWeixin() {
		List<Menu> allMenusToWeixin = this.service.getAllMenusToWeixin() ;
		
		Map<String, List<Menu>> map = new HashMap<String, List<Menu>>() ;
		map.put("button", allMenusToWeixin) ;
		
		ResponseMsg msg = WeixinUtil.doPost(Api.get(Api.MENU_CREATE_URL, WeixinContext.getAccessToken()), map) ;
		
		if(msg.getErrcode() > 0) {
			logger.info("微信菜单推送失败：{}", msg.toString());
			return ResponseResult.build(false, msg.getErrcode() + ":" +msg.getErrmsg()) ;
		} else {
			logger.info("微信菜单推送成功：{}", msg.toString());
			return ResponseResult.build(true, msg.getErrmsg()) ;
		}
	}

}
